﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace botliga
{
    public class Util
    {
        static public List<Byte> UTIL_CreateByteArray(Byte[] a, int size)
        {
            if (size < 1)
                return new List<Byte>();

            return new List<Byte>(a);
        }
        static public List<Byte> UTIL_ExtractCString(List<Byte> b, uint start)
        {
            // start searching the byte array at position 'start' for the first null value
            // if found, return the subarray from 'start' to the null value but not including the null value

            if (start < b.Count)
            {
                for (int i = (int)start; i < b.Count; ++i)
                {
                    if (b[i] == 0)
                        return b.GetRange((int)start, (int)(i - start));
                }

                // no null value found, return the rest of the byte array

                return new List<Byte>(b);
            }

            return new List<Byte>();
        }
        static public UInt32 UTIL_ByteArrayToUInt32(List<Byte> b, bool reverse, uint start = 0)
        {
            if (b.Count < start + 4)
                return 0;

            List<Byte> temp = b.GetRange((int)start, 4);

            if (reverse)
                temp.Reverse();

            return (UInt32)(temp[3] << 24 | temp[2] << 16 | temp[1] << 8 | temp[0]);
        }
        static public void UTIL_AppendByteArray(List<byte> b, List<byte> append)
        {
            b.AddRange(append);
        }
        static public void UTIL_AppendByteArray(List<byte> b, Byte[] a, int size)
        {
            UTIL_AppendByteArray(b, UTIL_CreateByteArray(a, size));
        }

        static public void UTIL_AppendByteArray(List<byte> b, string append, bool terminator)
        {
            // append the string plus a null terminator
            ASCIIEncoding enc = new ASCIIEncoding();
            var x = enc.GetBytes(append);
            b.AddRange(x);

            if (terminator)
                b.Add(0);
        }
        static public void UTIL_AppendByteArrayFast(List<byte> b, string append)
        {
            ASCIIEncoding enc = new ASCIIEncoding();
            var x = enc.GetBytes(append);
            b.AddRange(x);
        }
        static public void UTIL_AppendByteArrayFast(List<byte> b, string append, bool terminator)
        {
            // append the string plus a null terminator
            ASCIIEncoding enc = new ASCIIEncoding();
            var x = enc.GetBytes(append);
            b.AddRange(x);

            if (terminator)
                b.Add(0);
        }

        public static void UTIL_AppendByteArrayFast(List<byte> b, byte[] p)
        {
            b.AddRange(p);
        }

        public static void UTIL_AppendByteArray(List<byte> b, short i, bool reverse)
        {
            UTIL_AppendByteArray(b, UTIL_CreateByteArray(i, reverse));
        }
        public static List<byte> UTIL_CreateByteArray(short i, bool reverse)
        {
            List<byte> result = new List<byte>();
            result.Add((Byte)i);
            result.Add((Byte)(i >> 8));

            if (reverse)
                result.Reverse();

            return result;
        }


        public static List<byte> UTIL_CreateByteArray(uint i, bool reverse)
        {
            List<byte> result = new List<byte>();
            result.Add((Byte)i);
            result.Add((Byte)(i >> 8));
            result.Add((Byte)(i >> 16));
            result.Add((Byte)(i >> 24));

            if (reverse)
                result.Reverse();
            return result;

        }


        public static void UTIL_AppendByteArray(List<byte> b, uint i, bool reverse)
        {
            UTIL_AppendByteArray(b, UTIL_CreateByteArray(i, reverse));
        }


        public static ushort UTIL_ByteArrayToUInt16(List<byte> b, bool reverse, uint start = 0)
        {
            if (b.Count < start + 2)
                return 0;

            List<byte> temp = b.GetRange((int)start, 2);

            if (reverse)
                temp.Reverse();

            return (UInt16)(temp[1] << 8 | temp[0]);
        }
    }
}
